package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.SignIn;
import cn.wolfcode.trip.base.service.ISignInService;
import cn.wolfcode.trip.base.util.JasonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 签到控制器
 *
 * @author Lyu
 */
@RestController
@RequestMapping("/signins")
public class SignInController {
    private final ISignInService signInService;

    @Autowired
    public SignInController(ISignInService signInService) {
        this.signInService = signInService;
    }

    /**
     * 新增签到记录
     * 资源:
     * 动作: POST
     * 参数:
     *
     * @param entity 记录对象
     * @return json
     */
    @PostMapping
    public Object insert(SignIn entity) {
        signInService.saveOrUpdate(entity);
        return new JasonResult();
    }

    /**
     * 查询用户签到记录
     * 资源:
     * 动作: GET
     * 参数:
     *
     * @param userId 用户id
     * @return json
     */
    @GetMapping("/{userId}")
    public Object get(@PathVariable Long userId) {
        return signInService.get(userId);
    }

    /**
     * 查询最新用户签到记录
     * 资源:
     * 动作: GET
     * 参数:
     *
     * @param userId 用户id
     * @return json
     */
    @GetMapping("/{userId}/state")
    public Object getCurrentState(@PathVariable Long userId, boolean state) {
        return signInService.selectCurrentState(userId);
    }

    /**
     * 更新积分
     */
    @PutMapping("/{id}")
    public Object updateIntegral(@PathVariable Long id, Integer integral) {
        JasonResult result = new JasonResult();
        try {
            signInService.updateIntegral(id, integral);
        } catch (Exception e) {
            result.mark("领取失败");
        }
        return result;
    }
}
